import cv2
import json

if __name__ == "__main__":
    image_bgr = cv2.imread(r"D:\data\xishajietou\weihai\1208\L_1.bmp")
    image_gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)
    ret, corners = cv2.findChessboardCorners(image_gray, (11,8), None, cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_NORMALIZE_IMAGE)

    new_corners = cv2.cornerSubPix(image_gray, corners, (11, 11), (-1, -1), (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1))

    print(new_corners)

    image_with_corners = cv2.drawChessboardCorners(image_bgr, (11,8), new_corners, ret)
    # 显示带有角点的图像
    cv2.namedWindow("corners",0);
    cv2.resizeWindow("corners", 640, 480);
    cv2.imshow('corners', image_with_corners)
    cv2.waitKey(5000)  # 等待按键按下
    cv2.destroyAllWindows()  # 关闭所有窗口

    data = {  
            'ret': new_corners.tolist()
        }
  
    # 写入到文件  
    with open('L_1.json', 'w') as f:  
        json.dump(data, f, indent=4)  # indent参数用于美化输出，增加可读性